package it.lefo.dateReminder.bo;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import it.lefo.dateReminder.helper.DbHelper;
import it.lefo.dateReminder.helper.Utilities;
import it.lefo.dateReminder.model.Contact;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateUtils;

/**
 * @author LEFO
 * ContactsRepository
 * Repository containing the business logic for Contact model
 * 
 */
public class ContactsRepository {
	
	private SQLiteDatabase _db;
	
	public ContactsRepository(){}
	
	public ContactsRepository(SQLiteDatabase db){		
		_db = db;
	}
	
	public Contact getContactById(Integer id){
		Contact c = null;
		try{
			String where = " id = " + id;
			Cursor myResult = _db.query("contacts", new String[]{"typeevent", "phone", "name","surname","nickname","birthdate","txtmessage","id"}, where, null, null, null, null);		
			if (myResult.moveToFirst()){
				c = new Contact(myResult);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return c;
	}
	
	/**
	 * Get all contacts contained in contacts table
	 * @return List of contact
	 */
	public List<Contact> getAllContacts(){
		Cursor myResult = _db.query("contacts", new String[]{"typeevent", "phone", "name","surname","nickname","birthdate","txtmessage","id"}, null, null, null, null, null);
		List<Contact> contacts = new ArrayList<Contact>();
		try{
    	if (myResult.moveToFirst()){
    		do{
    			Contact c = new Contact(myResult);
    			contacts.add(c);
    		}while(myResult.moveToNext());
    	}
		}catch(Exception e){
			e.printStackTrace();
		}
    	return contacts;
	}
	
	/**
	 * Get all contacts with the specified birthday
	 * @param dt: Birthday
	 * @return List of contact
	 */
	public List<Contact> getAllContactsInDate(Date date){
		String dt = Utilities.DateToString(date, "dd/MM/yyyy");
		String where = "birthdate = '" + dt + "'"; 
		Cursor myResult = _db.query("contacts", new String[]{"typeevent", "phone", "name","surname","nickname","birthdate","txtmessage","id"}, where, null, null, null, null);
		List<Contact> contacts = new ArrayList<Contact>();
    	if (myResult.moveToFirst()){
    		do{
    			Contact c = new Contact(myResult);
    			contacts.add(c);
    		}while(myResult.moveToNext());
    	}
    	return contacts;
	}
	
	/**
	 * Get all contacts with the specified birthday
	 * @param dt: Birthday
	 * @return List of contact
	 */
	public List<Contact> getAllBirthday(Date date){
		String dt = Utilities.DateToString(date, "dd/MM");
		String where = "birthdate LIKE '%" + dt + "%'"; 
		Cursor myResult = _db.query("contacts", new String[]{"typeevent", "phone", "name","surname","nickname","birthdate","txtmessage","id"}, where, null, null, null, null);
		List<Contact> contacts = new ArrayList<Contact>();
    	if (myResult.moveToFirst()){
    		do{
    			Contact c = new Contact(myResult);
    			contacts.add(c);
    		}while(myResult.moveToNext());
    	}
    	return contacts;
	}
	
	/**
	 * Delete the contact with the id passed 
	 * @param id: id fo the contact to delete
	 */
	public void delete(Integer id){      	
		String where = "ID=" +id;
		_db.delete("contacts", where, null);
	}
}
